package service.impl.v2;

import java.util.List;
import java.util.Map;

import org.apache.commons.collections.map.HashedMap;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import dao.v2.ApplyReturnVOMapper;
import dao.v2.ConfigVOMapper;
import dao.v2.MemberCartVOMapper;
import dao.v2.OrderVOMapper;
import dao.v2.TimerVOMapper;
import model.JsonVO;
import model.LogOrderBack;
import model.OrderGoodVO;
import model.OrderTimer;
import model.OrderVO;
import service.v2.OrderCancelService;
import service.v2.PayService;
import service.v2.SplitService;
import service.v2.TimerService;
import util.HttpUtil;
@Service
public class TimerServiceImpl implements TimerService{
	
	@Autowired
	private TimerVOMapper dao;
	@Autowired
	private ConfigVOMapper configVOMapper;
	@Autowired
	private SplitService splitService;
	@Autowired
	private OrderVOMapper orderVOMapper;
	@Autowired
	private ApplyReturnVOMapper applyReturnVOMapper;
	@Autowired
	private MemberCartVOMapper memberCartVOMapper;
	@Autowired
	private OrderCancelService orderCancelService;
	@Autowired
	private PayService payService;
	
	/*
	 * 	order_timer_id 定时器ID
	 * 	order_id       订单ID
	 * 	order_back_id  退货ID 
	 * 	timer_state    0 订单 1 退货
	 * 	senconds       秒数
	 * 	type           定时器TYPE
	 */
	//发送倒计时请求
	public void orderPayTimer(String order_timer_id,String order_id,String order_back_id,String timer_state,String seconds,String type) {
	
		String token = configVOMapper.loadConfig("INTERFACE_TOKEN");
		String params = "order_timer_id="+order_timer_id+"&order_id="+order_id+"&order_back_id="+order_back_id+
						"&timer_state="+timer_state+"&seconds="+seconds+"&type="+type;
	
		String requestUrl = "http://127.0.0.1:3000/timer/setTimer";
		System.out.println("定时器发送****************************************"+order_timer_id);
		JsonVO json = HttpUtil.HttpRequest(requestUrl, HttpMethod.GET, "token="+token, params);
		
		if(json!=null){
			//正好token被修改
			if(json.getState()==403){	
				try {
					token = configVOMapper.loadConfig("INTERFACE_TOKEN");
					Thread.sleep(1000);
					 json = HttpUtil.HttpRequest(requestUrl, HttpMethod.GET, "token="+token, params);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}

	//接收定时器回调
	public boolean recieveTimer(String data) throws Exception {
		// TODO Auto-generated method stub
		
		JSONObject obj = JSON.parseObject(data);
		String order_timer_id =  obj.get("order_timer_id").toString();
		String order_id 	  		 =  obj.get("order_id").toString();
		String order_back_id  =  obj.get("order_back_id").toString();
		String timer_state      =  obj.get("timer_state").toString();
		String type 		         =  obj.get("type").toString();
		
		
		System.out.println("定时器回调****************************************"+order_timer_id);
		System.out.println("order_id"+order_id);
		System.out.println("order_back_id"+order_back_id);
		System.out.println("timer_state"+timer_state);
		System.out.println("type"+type);
		
		OrderTimer timer = dao.getOrderTimerById(order_timer_id);
		
		if(timer==null){
			return false;
		}
		
		else{
			OrderVO order = orderVOMapper.getOrderById(order_id);
			//订单流程
			if("0".equals(timer_state)){
				//7天自动确认收货
				if("1".equals(type)){
					orderCancelService.confirmRecieve(order_id, order_timer_id, order.getTicket_status(), order.getOrder_type());
				}
				//1小时未付款
				else if("4".equals(type)){
					orderCancelService.OrderCalcelNoPay(order_id, order_timer_id, order.getTicket_status(), order.getPay_type(), order.getCreate_by());
				}
			}
			//退货流程
			else if("1".equals(timer_state)){
				//14天未收到商品
				if("2".equals(type)){
					orderCancelService.neverRecieve14Days(order_id, order_back_id, order.getCreate_by(), timer.getTime());
				}				
			}
			return true;
		}
	}

	public List<OrderTimer> getAllTimer() {
		// TODO Auto-generated method stub
		return dao.getAllTimer();
	}
	
	public OrderTimer getOrderTimerByOrderIdAndState(String order_id,String type){
		return dao.getOrderTimerByOrderIdAndState(order_id, type);
	}

	public void updTest() {
		 orderCancelService.updtest();
	}
	
}
